
package gunglad.com.utils
import scala.util.parsing.json.JSON._
import scala.io.Source
//import scala.util.Using
import scala.util.Try._
object JsonUtil {
  def main(args: Array[String]): Unit = {

    //    var tt =  Map.empty[String, Any]

    //    D:\proj\hadoop\spark_demo_code\spark\db.json
    val path = "D:\\proj\\hadoop\\spark_demo_code\\spark\\db.json"
    //   val tt : Map[String, Any] = ScalaJsonParse.jsonFileToMap(path)
    val tt = JsonUtil.jsonFileToMap(path)

    //    val tree = parseFull(Source.fromFile("/data/result.json").mkString)
    //    val tree = parseFull(Source.fromFile(path).mkString)
    //    tt = tree match {
    //      case Some(map: Map[String, Any]) => map
    //    }
    //    println("tree")
    //    println(tree)
    //    tree.foreach(f=>println(f))

    //    println( tree["username"])
    //    println(tt.getClass.getSimpleName)

    val username = tt("username")
    val password = tt("password")
    val pp = tt("pp")
//    tt.
//    Exception in thread "main" java.util.NoSuchElementException: key not found: pp

    println("pp")
    println(pp)

    println("username")
    //    println( tt.get("username"))
    println(username)

    //    scala map get
    println("password")
    //    println( tt.get("password"))
    println(password)

    //    tt("username")

  }

//  def jsonFileToMap(path: String): Map[String, Any] = {
  def  jsonFileToMap(path: String): Map[String, String] = {
    //    val path="D:\\proj\\hadoop\\spark_demo_code\\spark\\db.json"
    //    val tree = parseFull(Source.fromFile("/data/result.json").mkString)
//    Source.fromFile(path).mkString
//    Source is not closed

//    us
//    Using(Source.fromFile(fileName).buffered) { reader =>
//      parseBufferedContent(reader)
//    }

//    Using

//    Caused by: scala.MatchError: None (of class scala.None$)
    val tree = parseFull(Source.fromFile(path).mkString)
    val res = tree match {
//      case Some(map: Map[String, Any]) => map
      case Some(map: Map[String, String]) => map
    }
    res
//    return res
  }
}
